package cn.h8k.hj212.example;

import cn.h8k.hj212.Hj212Codec;
import cn.h8k.hj212.factory.Hj212CodecFactory;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * HJ212协议编解码器使用示例
 */
public class Hj212Example {
    
    public static void main(String[] args) {
        // 示例1：使用默认编解码器
        defaultCodecExample();
        
        // 示例2：使用无CRC校验的编解码器
        noCrcCheckExample();
        
        // 示例3：解析实际协议数据
        parseRealProtocolExample();
    }
    
    /**
     * 使用默认编解码器的示例
     */
    private static void defaultCodecExample() {
        System.out.println("===== 默认编解码器示例 =====");
        
        // 使用工厂创建默认编解码器
        Hj212Codec codec = Hj212CodecFactory.createDefaultCodec();
        
        // 构建数据
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("QN", Hj212Codec.getTimeStamp());
        data.put("ST", "31");
        data.put("CN", "2011");
        data.put("PW", "123456");
        data.put("MN", "V130427GDSJ001");
        data.put("Flag", "4");
        
        Map<Integer, Map<String, Object>> cp = new LinkedHashMap<>();
        
        Map<String, Object> cp0 = new LinkedHashMap<>();
        cp0.put("DataTime", data.get("QN").toString().substring(0, 14));
        cp.put(0, cp0);
        
        Map<String, Object> cp1 = new LinkedHashMap<>();
        cp1.put("avoc01-Rtd", 0.021);
        cp1.put("avoc01-Flag", "N");
        cp.put(1, cp1);
        
        data.put("CP", cp);
        
        // 编码
        String encoded = codec.encode(data);
        System.out.println("编码结果: " + encoded);
        
        // 解码
        Map<String, Object> decoded = codec.decode(encoded);
        System.out.println("解码结果: " + decoded);
        
        // 访问解码后的数据
        @SuppressWarnings("unchecked")
        Map<String, Object> decodedData = (Map<String, Object>) decoded.get("DATA");
        System.out.println("设备MN: " + decodedData.get("MN"));
        
        @SuppressWarnings("unchecked")
        Map<Integer, Map<String, Object>> decodedCp = (Map<Integer, Map<String, Object>>) decodedData.get("CP");
        System.out.println("数据时间: " + decodedCp.get(0).get("DataTime"));
        System.out.println("avoc01-Rtd: " + decodedCp.get(1).get("avoc01-Rtd"));
        System.out.println("avoc01-Flag: " + decodedCp.get(1).get("avoc01-Flag"));
    }
    
    /**
     * 使用无CRC校验的编解码器示例
     */
    private static void noCrcCheckExample() {
        System.out.println("\n===== 无CRC校验编解码器示例 =====");
        
        // 使用工厂创建无CRC校验的编解码器
        Hj212Codec codec = Hj212CodecFactory.createNoCrcCheckCodec();
        
        // 构造一个CRC错误的协议字符串
        String wrongCrcData = "##0131QN=20210730012315231;ST=31;CN=2011;PW=123456;MN=V130427GDSJ001;Flag=4;CP=&&DataTime=20210730012315;avoc01-Rtd=0.021,avoc01-Flag=N&&FFFF\r\n";
        
        try {
            // 解码（不会因为CRC错误而抛出异常）
            Map<String, Object> decoded = codec.decode(wrongCrcData);
            System.out.println("成功解码CRC错误的数据: " + decoded.get("CRC"));
        } catch (Exception e) {
            System.out.println("解码失败: " + e.getMessage());
        }
    }
    
    /**
     * 解析实际协议数据示例
     */
    private static void parseRealProtocolExample() {
        System.out.println("\n===== 解析实际协议数据示例 =====");
        
        // 使用工厂创建默认编解码器
        Hj212Codec codec = Hj212CodecFactory.createDefaultCodec();
        
        // 实际的协议数据
        String realData = "##0131QN=20210730012315231;ST=31;CN=2011;PW=123456;MN=V130427GDSJ001;Flag=4;CP=&&DataTime=20210730012315;avoc01-Rtd=0.021,avoc01-Flag=N&&1A40\r\n";
        
        try {
            // 解码
            Map<String, Object> decoded = codec.decode(realData);
            
            @SuppressWarnings("unchecked")
            Map<String, Object> decodedData = (Map<String, Object>) decoded.get("DATA");
            System.out.println("协议版本: " + decodedData.get("ST"));
            System.out.println("命令编码: " + decodedData.get("CN"));
            System.out.println("设备编码: " + decodedData.get("MN"));
            
            @SuppressWarnings("unchecked")
            Map<Integer, Map<String, Object>> decodedCp = (Map<Integer, Map<String, Object>>) decodedData.get("CP");
            System.out.println("数据时间: " + decodedCp.get(0).get("DataTime"));
            System.out.println("avoc01-Rtd: " + decodedCp.get(0).get("avoc01-Rtd"));
            System.out.println("avoc01-Flag: " + decodedCp.get(0).get("avoc01-Flag"));
        } catch (Exception e) {
            System.out.println("解码失败: " + e.getMessage());
        }
    }
} 